package views;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.*;

import controllers.VehicleType;
import db.VehicleTypeDAO;

public class AddVehicleTypeFrame extends JFrame {
	
	private static final int WIDTH=400;
	private static final int HEIGHT=300;
	private static final String TITLE = "Add Vehicle Type";
	private static JTextField name, numOfSeats;
	private static JButton save;
	
	public AddVehicleTypeFrame(int xBound, int yBound){
		this.setBounds(xBound,yBound , WIDTH, HEIGHT);
		this.setResizable(false);
		this.setTitle(TITLE);
		
		KeyListener l = new KeyListener(){
			@Override
			public void keyPressed(KeyEvent arg0) {
				enableSave();	
			}
			@Override
			public void keyReleased(KeyEvent arg0) {
				enableSave();
				
			}
			@Override
			public void keyTyped(KeyEvent arg0) {
				enableSave();
			}
		};
		
		JPanel panel = new JPanel();
		panel.setLayout(null);
		Container c = this.getContentPane();
		
		JLabel nameLabel = new JLabel("Vehicle Type Name");
		nameLabel.setBounds(25, 25, 200, 25);
		panel.add(nameLabel);
		
		name = new JTextField();
		name.setBounds(25, 50, 200, 25);
		name.addKeyListener(l);
		panel.add(name);
		
		JLabel numOfSeatsLabel = new JLabel("Number of Seats");
		numOfSeatsLabel.setBounds(25, 75, 200, 25);
		panel.add(numOfSeatsLabel);
		
		numOfSeats = new JTextField();
		numOfSeats.setBounds(25, 100, 200, 25);
		numOfSeats.addKeyListener(l);
		panel.add(numOfSeats);
		
		save = new JButton("Save");
		save.setBounds(25, 135, 100, 25);
		save.setEnabled(false);
		save.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				VehicleType vt = new VehicleType();
				vt.setName(name.getText());
				vt.setNumOfSeats(Integer.parseInt(numOfSeats.getText()));
				VehicleTypeDAO.addVehicleType(vt);
				ViewVehicleTypesFrame.updateRows();
				AddVehicleTypeFrame.this.dispose();
			}
		});
		panel.add(save);
		
		c.add(panel);
		
		this.setVisible(true);
	}
	
	public void enableSave(){
		if(name.getText().length()>0&&numOfSeats.getText().length()>0){
			save.setEnabled(true);
		}
		else{
			save.setEnabled(false);
		}
	}

}
